//
//  SCPhotoPreviewViewCell.m
//  PhotoAlbum
//
//  Created by Chao Sheng on 2021/12/6.
//

#import "SCPhotoPreviewViewCell.h"

@interface SCPhotoPreviewViewCell () <UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView *scrollView;

@end

@implementation SCPhotoPreviewViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    [self.contentView addSubview:self.scrollView];
    [self.scrollView addSubview:self.imageView];
}

- (void)setPhotoModel:(SCPhotoModel *)photoModel {
    _photoModel = photoModel;
    
    sc_weak(self);
    if (_photoModel.highQualityImage) {
        
        self.imageView.image = _photoModel.highQualityImage;
        
    } else if(_photoModel.imageNetworkingUrl) {
        
        if (_photoModel.downloadError) {
            self.imageView.image = _photoModel.tempImage;
        } else {
            [self.imageView sc_setImageWithModel:_photoModel progress:^(CGFloat progress, SCPhotoModel * _Nonnull model) {
            } completed:^(UIImage * _Nonnull image, NSError * _Nonnull error, SCPhotoModel * _Nonnull model) {
                weakself.imageView.image = image;
            }];
        }
        
    } else if(_photoModel.asset) {
        
        CGSize targetSize = CGSizeMake(_photoModel.imgSize.width, _photoModel.imgSize.height);
        [SCPhotoTools getPhotoForPHAsset:_photoModel.asset size:targetSize completion:^(UIImage * _Nonnull image,PHAsset *asset) {
            if (weakself.photoModel.asset==asset) {
                weakself.imageView.image = image;
                weakself.photoModel.highQualityImage = image;
            }
        }];
    } else {
        
        self.imageView.image = [UIImage sc_bundleImageNamed:@"LGQ-placeholderImg"];
    }
    
    [self refreshFrame];
}

- (void)refreshFrame {
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    CGFloat imgWidth = _photoModel.imgSize.width;
    CGFloat imgHeight = _photoModel.imgSize.height;
    CGFloat w;
    CGFloat h;
    
    imgHeight = width / imgWidth * imgHeight;
    if (imgHeight > height) {
        w = height / _photoModel.imgSize.height * imgWidth;
        h = height;
        self.scrollView.maximumZoomScale = width / w + 0.5;
    }else {
        w = width;
        h = imgHeight;
        self.scrollView.maximumZoomScale = 2.5;
    }
    self.imageView.frame = CGRectMake(0, 0, w, h);
    self.imageView.center = CGPointMake(width / 2, height / 2);
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.scrollView.frame = self.bounds;
    self.scrollView.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height);
}

#pragma mark - action
// 单击查看（收起导航栏和工具栏）
- (void)singleTap:(UIGestureRecognizer*)sender {
    if (self.scCellTapClick) {
        self.scCellTapClick();
    }
}

// 双击缩放
- (void)doubleTap:(UIGestureRecognizer*)sender {
    if (self.scrollView.zoomScale > 1.0) {
        [self.scrollView setZoomScale:1.0 animated:YES];
    } else {
        CGPoint touchPoint = [sender locationInView:self.imageView];
        CGFloat newZoomScale = self.scrollView.maximumZoomScale;
        CGFloat xsize = self.frame.size.width / newZoomScale;
        CGFloat ysize = self.frame.size.height / newZoomScale;
        [self.scrollView zoomToRect:CGRectMake(touchPoint.x - xsize/2, touchPoint.y - ysize/2, xsize, ysize) animated:YES];
    }
}

#pragma mark - scroll delegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return self.imageView;//实现缩放必须调用
}

#pragma mark - 懒加载
- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
    }
    return _imageView;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.bouncesZoom = YES;
        _scrollView.minimumZoomScale = 1;
        _scrollView.multipleTouchEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.scrollsToTop = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _scrollView.delaysContentTouches = NO;
        _scrollView.canCancelContentTouches = YES;
        _scrollView.alwaysBounceVertical = NO;
        _scrollView.userInteractionEnabled = true;
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
        [_scrollView addGestureRecognizer:tap1];
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
        tap2.numberOfTapsRequired = 2;
        [tap1 requireGestureRecognizerToFail:tap2];
        [_scrollView addGestureRecognizer:tap2];
    }
    return _scrollView;
}

@end
